<?php
namespace app\model;
use app\api\controller\Aliyuniot;
use app\BaseModel;
use app\model\Project;
use think\model\concern\SoftDelete;
use app\model\LeasingCompany;

class Device extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort','status'];
    protected $append = ['online_status'];


    public function searchProjectIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('project_id', '=', $value);
        }
    }
    public function searchTitleAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('title|id_no|name_no', 'like', '%'.$value.'%');
        }
    }

    public function searchIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('id', '=', $value);
        }
    }

    public function searchCompanyIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('company_id', '=', $value);
        }
    }

    public function searchOnlineStatusAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('online_status', '=', $value);
        }
    }



    protected function setProjectIdAttr($value,$data){ //当前字段的修改器
         $this->set('project_title',Project::where('id',$value)->value('title'));
        $this->set('rent_price',Project::where('id',$value)->value('rent_price'));
         return $value;
    }
    protected function setIsRentAttr($value,$data){ //当前字段的修改器
        $status = [0=>'错误','1'=>'停租','2'=>'启租'];
        $this->set('is_rent_title',$status[$value]);
        return $value;
    }
    protected function setCompanyIdAttr($value,$data){ //当前字段的修改器
         $this->set('company_title',LeasingCompany::where('id',$value)->value('title'));
         return $value;
    }



//    protected function getDeviceDetailAttr($value,$data)
//    {
//        $ret_data = Aliyuniot::device_detail($data['title']);
//
//        $ret = [];
//        if($ret_data->status = 'ONLINE'){
//            $ret['status'] = '在线';
//        }
//        if($ret_data->status == 'OFFLINE')
//        {
//            $ret['status'] = '离线';
//        }
//       //$ret['ip'] = $ret_data->IpAddress;
//        return $ret;
//    }

    protected function getQrcodeAttr($value, $data)
    {
        $ret = qrcode($data['title'],'device/'.$data['title'].'.png')['url'].'?rand='.mt_rand(1,100);

        return $ret;
    }

    protected function getOnlineStatusAttr($value,$data)
    {
        $ret_data = Aliyuniot::device_detail($data['title']);

         $ret = [];
        if(isset($ret_data->status) && $ret_data->status == 'ONLINE'){
            return '在线';
        }
        if(isset($ret_data->status) && $ret_data->status == 'OFFLINE')
        {
            self::where('id',$data['id'])->update(['remote_status'=>'关机','error'=>'','runtime'=>0]);
            return '离线';
            $ret['status'] = '离线';
        }
        if(!isset($ret_data->status)){
            return '-';
    }
       //$ret['ip'] = $ret_data->IpAddress;

        return $ret_data->status;
    }

//    public function getUseStatusAttr($value,$data)
//    {
//        $status = [''];
//    }





}